package org.jackysoft.report.visitor;

import javax.annotation.Resource;

import org.jackysoft.entity.ArmItem;
import org.jackysoft.entity.DataItem;
import org.jackysoft.report.ItemUnit;
import org.jackysoft.report.DataItemType;
import org.jackysoft.report.DataItemUtil;
import org.jackysoft.report.ReporterUtil;

public abstract class AbstractArmItemVisitor implements ArmItemVisitor {

	protected ArmItem source;
	protected DataItemUtil dataItemUtil;
	protected ReporterUtil reportUtil;
		
	public AbstractArmItemVisitor() {
		super();
	}

	public AbstractArmItemVisitor(ArmItem source) {
		this.source = source;
	}

	@Override
	public Double getValue() throws NoSuchMethodException {
		return null;
	}

	@Override
	public void visite(ItemUnit item) {

	}

	public void setSource(ArmItem source) {
		this.source = source;
	}

	public ArmItem getSource() {
		return source;
	}
	
	
	/**
	 * 用于统计数据项当月到累计
	 * @param dt 数据项类型
	 * @param source 源数据项
	 * @param target 目标数据项
	 * 
	 * */
	protected void accumulate(DataItemType dt,ArmItem source,ArmItem target){
		DataItem sourceData = getDataItem(source,dt);
		DataItem targetData = getDataItem(target,dt);
		if(sourceData==null)sourceData = new DataItem();
		if(targetData==null)targetData = new DataItem();
		targetData.setAccumulateValue(targetData.getAtomValue()+sourceData.getAccumulateValue());
	}
	
	/**
	 * 用于统计数据项当月到累计
	 * @param dt 数据项类型
	 * @param source 源数据项
	 * @param target 目标数据项
	 * 
	 * */
	protected void differ(DataItemType dt,ArmItem source,ArmItem target){
		DataItem sourceData = getDataItem(source,dt);
		DataItem targetData = getDataItem(target,dt);
		if(sourceData==null)sourceData = new DataItem();
		if(targetData==null)targetData = new DataItem();
		targetData.setAtomValue(targetData.getAtomValue()-sourceData.getAtomValue());
		targetData.setAccumulateValue(targetData.getAccumulateValue()-sourceData.getAccumulateValue());
	}
	
	/**
	 * 用于统计数据项当月到累计
	 * @param dt 数据项类型
	 * @param source 源数据项
	 * @param target 目标数据项
	 * 
	 * */
	protected void merge(DataItemType dt,ArmItem source,ArmItem target){
		DataItem sourceData = getDataItem(source,dt);
		DataItem targetData = getDataItem(target,dt);
		if(sourceData==null)sourceData = new DataItem();
		if(targetData==null)targetData = new DataItem();
		targetData.setAtomValue(targetData.getAtomValue()+sourceData.getAtomValue());
		targetData.setAccumulateValue(targetData.getAccumulateValue()+sourceData.getAccumulateValue());
	}
	
	protected DataItem getDataItem(ArmItem ai,DataItemType dt){
		return ai.getDataItems().get(dt.toString());
	}
	
	protected Double getAtom(ArmItem ai ,DataItemType dt){
		DataItem di = getDataItem(ai,dt);
		return di==null?0D:getAtom(di);
	}
	
	protected Double getAccumulation(ArmItem ai ,DataItemType dt){
		DataItem di = getDataItem(ai,dt);
		return di==null?0D:getAccumulation(di);
	}
	
	protected Double getAtom(DataItem d){
		return d.getAtomValue();
	}
	protected Double getAccumulation(DataItem d){
		Double val = d.getAccumulateValue();
		return val==null?0D:val;
	}

	
	
	
	
	@Resource
	public void setDataItemUtil(DataItemUtil dataItemUtil) {
		this.dataItemUtil = dataItemUtil;
	}

	@Resource
	public void setReportUtil(ReporterUtil reportUtil) {
		this.reportUtil = reportUtil;
	}
}
